//
//  MyCouponsVC.m
//  BrushFace
//
//  Created by timer_q on 16/1/19.
//  Copyright © 2016年 Zzz. All rights reserved.
//

#import "MyCouponsVC.h"
#import "MyConCell.h"
#import "NavHelper.h"

@interface MyCouponsVC ()<UITableViewDataSource,UITableViewDelegate>
{
    UITableView *_tableView;
    NSMutableArray *_dataArray;
    UILabel *headerlabel;
    NSDictionary *_currentDict;
}

@end

@implementation MyCouponsVC

- (void)viewDidLoad{
    [super viewDidLoad];
    [self UIConfig];
    [self dataInit];
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStyleGrouped]
    ;
    _tableView.backgroundColor = UIColorFromRGB(ColorValueForBackView_Gray);
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
}

- (void)UIConfig{
    [self.navBar setTitle:@"优惠券"];
    [self.navBar setLeftBtn:NAV_BAR_LEFT_BTN_TYPE_BLUE];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
    
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 20, 80, 44)];
    [button setTitle:@"使用说明" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:13];
    [button addTarget:self action:@selector(rightBtnClikc) forControlEvents:UIControlEventTouchUpInside];
    [self.navBar setRightView:button];

}
- (void)dataInit{
    _dataArray = [[NSMutableArray alloc] init];
    NSString *requestStr = [NSString stringWithFormat:@"%@/coupons/listusercoupons.json",SERVICE_ADRESS];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:requestStr parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        NSDictionary *jsondict = [NSJSONSerialization JSONObjectWithData:operation.responseData options:0 error:nil];
        _currentDict = jsondict;
        [_dataArray removeAllObjects];
        [_dataArray addObjectsFromArray:jsondict[@"coupons"]];
        [_tableView reloadData];

    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        
    }];
}

#pragma mark - tableviewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MyConCell *cell = [MyConCell loadBundleNib];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSDictionary *subDict = [_dataArray objectAtIndex:indexPath.row];
    cell.titlelabel.text = subDict[@"name"];
    NSString *limitstr = subDict[@"limitstr"];
    cell.moneyLabel.text = [NSString stringWithFormat:@"¥%@",[subDict objectForKey:@"couponsvalue"]];
    NSString *finalStr = [limitstr stringByReplacingOccurrencesOfString:@"<br>" withString:@"\n"];
    cell.limitLabel.text = finalStr;
    cell.dateLabel.text = [NSString stringWithFormat:@"%@",subDict[@"validity"]];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
    view.backgroundColor = [UIColor clearColor];
    headerlabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 5, SCREEN_WIDTH, 35)];
    headerlabel.font = [UIFont systemFontOfSize:14];
    headerlabel.textColor = [UIColor grayColor];
    headerlabel.textAlignment = NSTextAlignmentCenter;
    if (_currentDict.count>0) {
        headerlabel.text = [NSString stringWithFormat:@"共%@张",_currentDict[@"countCoupons"]];
    }
    [view addSubview:headerlabel];
    
    UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(0, 39, SCREEN_WIDTH, 1)];
    line.textColor = [UIColor lightGrayColor];
    [view addSubview:line];
    return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 120;
}
- (void)rightBtnClikc{
    [NavHelper gotoInstructionsWithConpousVC];


}
@end
